package function;

import gui.TextEditor;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class FunctionHandler {
	TextEditor gui;
	//The name of the currently open file
	String filename = "";

	public FunctionHandler(TextEditor gui){
		this.gui = gui;
	}

	public void handleAction(String cmd) {

		if(cmd == "new"){
			openNew();
		}else if(cmd == "open"){
			openFileChooser();
		}else if(cmd == "save"){
			save();
		}else if(cmd == "exit"){
			exit();
		}else if(cmd == "cut"){
			cut();
		}else if(cmd == "copy"){
			copy();
		}else if(cmd == "paste"){
			paste();
		}else if(cmd == "text"){
			text();
		}else if(cmd == "background"){
			background();
		}else if(cmd == "toggle"){
			toggle();
		}
	}

	private void openNew(){
		gui.getTextPane().setText("");
		filename = "";
	}
	private void openFileChooser(){
		JFileChooser jfc = new JFileChooser();
	      int result = jfc.showOpenDialog(gui);
	      if(result == JFileChooser.CANCEL_OPTION) return;
	      try {
	         File file = jfc.getSelectedFile();
	         filename = jfc.getSelectedFile().getName();
	         BufferedReader br = new BufferedReader(new FileReader(file));
	         String s=""; int c=0;
	         while((c=br.read())!=-1) s+=(char)c;
	         br.close(); gui.getTextPane().setText(s);
	      } catch (Exception e) {
	         JOptionPane.showMessageDialog(gui,e.getMessage(),
	         "File error",JOptionPane.ERROR_MESSAGE);}
	      gui.setTitle(filename);
	}
	
	private void save(){
		JFileChooser jfc = new JFileChooser();
	      int result = jfc.showSaveDialog(gui);
	      if(result == JFileChooser.CANCEL_OPTION) return;
	      File file = jfc.getSelectedFile();
	      try {
	         BufferedWriter bw = new BufferedWriter(new FileWriter(file));
	         bw.write(gui.getTextPane().getText());
	         bw.close();
	      }
	      catch (Exception e) {
	         JOptionPane.showMessageDialog(
	            gui,
	            e.getMessage(),
	            "File Error",
	            JOptionPane.ERROR_MESSAGE
	         );
	      }
	}
	private void exit(){
		System.exit(0);
	}
	private void cut(){
		gui.getTextPane().cut();
	}
	private void copy(){
		gui.getTextPane().copy();
	}
	private void paste(){
		gui.getTextPane().paste();
	}
	private void text(){
		System.out.println("pick a text color");
		JColorChooser chooser = new JColorChooser();
		chooser.setColor(Color.BLACK);
		Color newColor = chooser.showDialog(gui,
				"Pick your dumb color.", gui.getBackground());
		gui.getTextPane().setForeground(newColor);
	}

	private void background(){
		System.out.println("pick a background color");
		JColorChooser chooser = new JColorChooser();
		chooser.setColor(Color.BLACK);
		Color newColor = chooser.showDialog(gui,
				"Pick your dumb color.", gui.getBackground());
		gui.getTextPane().setBackground(newColor);
	}
	private void toggle(){
		if(gui.getToolBar().isVisible()){
			gui.getToolBar().setVisible(false);
			gui.getScrollPane().setBounds(0, 0, 792, 498+51);
		} else{
			gui.getToolBar().setVisible(true);
			gui.getScrollPane().setBounds(0, 51, 792, 498);
		}
	}



}
